package com.ali.common;

import org.springframework.util.FileCopyUtils;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileWriteUtil {
    /**
     * 目录写入
     */
    public static void directoryFileWrite(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    /**
     * 文件写入 - FileWriter
     */
    public static void fileWriterFileWrite(String filePath, String content) {
        try (FileWriter fileWritter = new FileWriter(filePath, true)) {
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }

            fileWritter.write(content);
            fileWritter.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 文件写入 - FileOutputStream
     */
    public static void fileOutputStreamFileRead(String filePath, String content) {
        try (FileOutputStream fileOutputStream = new FileOutputStream(filePath)) {
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }

            fileOutputStream.write(content.getBytes(StandardCharsets.UTF_8));
            fileOutputStream.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 文件写入 - BufferedWriter
     */
    public static void bufferedWriterFileWrite(String filePath, String content) {
        try (FileWriter fileWritter = new FileWriter(filePath, true); BufferedWriter bufferedWriter = new BufferedWriter(fileWritter)) {
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }

            bufferedWriter.write(content);
            bufferedWriter.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 文件复制 - OutputStream
     */
    public static void outputStreamFileCopy(String filePath, String newFilePath) {
        File file = new File(filePath);
        File newFile = new File(newFilePath);

        try (InputStream inputStream = new FileInputStream(file); OutputStream outputStream = new FileOutputStream(newFile)) {
            byte[] bytes = new byte[1024];
            int length;
            while ((length = inputStream.read(bytes)) > 0) {
                outputStream.write(bytes, 0, length);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 文件复制 - FileChannel
     */
    public static void fileChannelFileCopy(String filePath, String newFilePath) {
        File file = new File(filePath);
        File newFile = new File(newFilePath);

        try (FileInputStream fileInputStream = new FileInputStream(file);
             FileChannel inFileChannel = fileInputStream.getChannel();
             FileOutputStream fileOutputStream = new FileOutputStream(newFile);
             FileChannel outFileChannel = fileOutputStream.getChannel()) {
            outFileChannel.transferFrom(inFileChannel, 0, inFileChannel.size());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 文件复制 - FileCopyUtils
     */
    public static void fileCopyUtilsFileCopy(String filePath, String newFilePath) {
        File file = new File(filePath);
        File newFile = new File(newFilePath);

        try {
            FileCopyUtils.copy(file, newFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 文件复制 - Files
     */
    public static void filesFileCopy(String filePath, String newFilePath) {
        Path path = Paths.get(filePath);
        Path newPath = Paths.get(newFilePath);

        try {
            Files.copy(path, newPath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}